package com.sansui.blog.service.impl;

import com.sansui.blog.dao.SysEmailDao;
import com.sansui.blog.dao.SysEmailLogDao;
import com.sansui.blog.pojo.sys.SysEmail;
import com.sansui.blog.pojo.sys.SysEmailLog;
import com.sansui.blog.service.SysEmailService;
import com.sansui.blog.utils.SendEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @name: SysEmailServiceImpl
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-11-25 17:45
 **/

@Service
public class SysEmailServiceImpl implements SysEmailService {

    @Autowired
    private SysEmailDao sysEmailDao;

    @Autowired
    private SysEmailLogDao sysEmailLogDao;

    /**
     * 获取系统发信配置
     * @return
     */
    @Override
    public SysEmail getSysEmail() {
        return sysEmailDao.getSysEmail();
    }

    @Transactional
    @Override
    public Map<String, Object> saveEmail(SysEmail sysEmail) {
        Map<String,Object> data = new HashMap<>(16);
        try{
            if(sysEmailDao.getSysEmail() != null){
                sysEmailDao.updateEmail(sysEmail);
                data.put("code",200);
                data.put("msg","保存成功！");
            } else {
                sysEmailDao.saveEmail(sysEmail);
                data.put("code",200);
                data.put("msg","保存成功！");
            }
        }catch (Exception e){
            data.put("code",400);
            data.put("msg","保存失败！");
        }
        return data;
    }

    //测试短信发送
    @Transactional
    @Override
    public Map<String, Object> setEmailDemo(String demoMail) {
        Map<String,Object> data = new HashMap<>(16);
        try{
            SysEmail sysEmail = sysEmailDao.getSysEmail();

            String emailContent = "<div>\n" +
                    "    <includetail>\n" +
                    "        <div align='center'>\n" +
                    "            <div class='open_email' style='margin-left: 8px; margin-top: 8px; margin-bottom: 8px; margin-right: 8px;'>\n" +
                    "                <div> <br> <span class='genEmailContent'>\n" +
                    "<div id='cTMail-Wrap' style='word-break: break-all;box-sizing:border-box;text-align:center;min-width:320px; max-width:660px; border:1px solid #f6f6f6; background-color:#f7f8fa; margin:auto; padding:20px 0 30px; font-family:'helvetica neue',PingFangSC-Light,arial,'hiragino sans gb','microsoft yahei ui','microsoft yahei',simsun,sans-serif'>\n" +
                    "<div class='main-content' style=''>\n" +
                    "<table style='width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse'>\n" +
                    "<tbody>\n" +
                    "<tr style='font-weight:300'>\n" +
                    "<td style='width:3%;max-width:30px;'></td>\n" +
                    "<td style='max-width:600px;'><h3>系统测试邮件</h3>\n" +
                    "<p style='height:2px;background-color: #00a4ff;border: 0;font-size:0;padding:0;width:100%;margin-top:20px;'></p>\n" +
                    "<div id='cTMail-inner' style='background-color:#fff; padding:23px 0 20px;box-shadow: 0px 1px 1px 0px rgba(122, 55, 55, 0.2);text-align:left;'>\n" +
                    "<table style='width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse;text-align:left;'>\n" +
                    "<tbody>\n" +
                    "<tr style='font-weight:300'>\n" +
                    "<td style='width:3.2%;max-width:30px;'></td>\n" +
                    "<td style='max-width:480px;text-align:left;'>\n" +
                    "<h1 id='cTMail-title' style='font-size: 20px; line-height: 36px; margin: 0px 0px 22px;'>\n" +
                    "当收到本邮件时，说明配置正确\n" +
                    "</h1>\n" +
                    "<dl style='font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;'>\n" +
                    "<dd style='margin: 0px 0px 6px; padding: 0px; font-size: 12px; line-height: 22px;'>\n" +
                    "<p id= 'cTMail-sender' style='font-size: 14px; line-height: 26px; word-wrap: break-word; word-break: break-all; margin-top: 32px;'>\n" +
                    "此致\n" +
                    "<br>\n" +
                    "<strong>博客运维团队</strong>\n" +
                    "</p>\n" +
                    "</dd>\n" +
                    "</dl>\n" +
                    "</td>\n" +
                    "<td style='width:3.2%;max-width:30px;'></td>\n" +
                    "</tr>\n" +
                    "</tbody>\n" +
                    "</table>\n" +
                    "</div>\n" +
                    "<div id='TMail-copy'style='text-align:center; font-size:12px; line-height:18px; color:#999'>\n" +
                    "<table style='width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse'>\n" +
                    "<tbody>\n" +
                    "<tr style='font-weight:300'>\n" +
                    "<td style='width:3.2%;max-width:30px;'></td>\n" +
                    "<td style='max-width:540px;'>\n" +
                    "<p style='text-align:center; margin:20px auto 14px auto;font-size:12px;color:#999;'>\n" +
                    "此为系统邮件，请勿回复。\n" +
                    "</p>\n" +
                    "</td>\n" +
                    "<td style='width:3.2%;max-width:30px;'></td>\n" +
                    "</tr>\n" +
                    "</tbody>\n" +
                    "</table>\n" +
                    "</div>\n" +
                    "</td>\n" +
                    "<td style='width:3%;max-width:30px;'></td>\n" +
                    "</tr>\n" +
                    "</tbody>\n" +
                    "</table>\n" +
                    "</div>\n" +
                    "</div>\n" +
                    "</span>\n" +
                    "                </div>\n" +
                    "            </div>\n" +
                    "        </div>\n" +
                    "    </includetail>\n" +
                    "</div>";

            if(sysEmail != null){
               Boolean isTrue = SendEmail.aTest(sysEmail.getMailSmtp(),sysEmail.getMailPort(),demoMail,sysEmail.getMailUser(),"[系统测试邮件]",emailContent,sysEmail.getMailName(),sysEmail.getMailPwd());

               if(isTrue){
                   SysEmailLog sysEmailLog = new SysEmailLog();
                   sysEmailLog.setSendMailUser(sysEmail.getMailUser());
                   sysEmailLog.setSendMailName(sysEmail.getMailName());
                   sysEmailLog.setSendComment(emailContent);
                   sysEmailLog.setSendAddresseeEmail(demoMail);
                   sysEmailLog.setSendMailSmtp(sysEmail.getMailSmtp());
                   sysEmailLog.setSendType("系统测试邮件");
                   sysEmailLog.setCreateTime(new Date());

                   sysEmailLogDao.saveEmailLog(sysEmailLog);
                   data.put("code",200);
                   data.put("msg","测试邮件发送已成功！");
               }else{
                   data.put("code",4002);
                   data.put("msg","失败，请检查发信配置是否正确！");
               }
            } else {
                data.put("code",4001);
                data.put("msg","失败，请检查发信配置是否填写完整！");
            }
        }catch (Exception e){
            e.printStackTrace();
            data.put("code",400);
            data.put("msg","测试邮件发送失败,请查看错误日志");
        }
        return data;
    }


}
